<template>
  <div>
    <el-dialog
      title="上/下架"
      :close-on-click-modal="false"
      :visible.sync="visible"
    >
      <el-form
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmit()"
        label-width="110px"
      >
        <el-form-item label="书籍名称" prop="bookName">
          <el-input v-model="dataForm.bookName" placeholder="书籍名称"></el-input>
        </el-form-item>
        <el-form-item label="书籍类型" prop="categoryd">
          <el-select  v-model="dataForm.categoryIds" multiple  placeholder="请选择" >
            <el-option
              v-for="item in projectList"
              :key="item.categoryd"
              :label="item.categoryName"
              :value="item.categoryd"
            >
            </el-option>
          </el-select>
          <!-- <el-input v-model="dataForm.categoryList" placeholder="书籍类型"></el-input> -->
        </el-form-item>
        <el-form-item label="是否推荐" prop="type">
          <el-radio v-model="dataForm.type" label="-1">不做推荐处理</el-radio>
          <el-radio v-model="dataForm.type" label="2">周推荐</el-radio>
          <el-radio v-model="dataForm.type" label="3">新书推荐</el-radio>
        </el-form-item>
        <el-form-item label="是否免费" prop="isFree">
          <el-radio v-model="dataForm.isFree" label="1">免费</el-radio>
          <el-radio v-model="dataForm.isFree" label="0">收费</el-radio>
        </el-form-item>
        <el-form-item label="初始访问量" prop="bookVisit">
          <el-input v-model="dataForm.bookVisit" placeholder="初始访问量"></el-input>
        </el-form-item>
        <el-form-item label="初始热度量" prop="bookHeat">
          <el-input v-model="dataForm.bookHeat" placeholder="初始热度量"></el-input>
        </el-form-item>
        <el-form-item label="上架时间" prop='isAlive'>
          <el-radio v-model="dataForm.isAlive" :label="1" :disabled="isAlive === 1">立即上架</el-radio>
          <el-radio v-model="dataForm.isAlive" :label="2" :disabled="isAlive === 1">定时上架</el-radio>
          <el-date-picker
            popper-class = 'dingTime'
            v-if="dataForm.isAlive === 2"
            v-model="dataForm.publishTime"
            type="datetime"
            placeholder="选择推送时间"
            style="margin-left: 20px;"
            @change="changeTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            :disabled="isAlive === 1"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="推荐文案" prop="info">
            <el-input type="textarea" :rows="5" v-model="dataForm.info" placeholder="推荐文案"></el-input>
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dataFormSubmit(1)" :disabled="isAlive === 1">上架</el-button>
        <el-button type="primary" @click="dataFormSubmit(0)" :disabled="(isAlive === 0 || dataForm.isAlive === 2) && isAlive !== 2">下架</el-button>
        <el-button @click="visible = false">取消</el-button>

      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      shelfVisible: false,
      visible: false,
      dataForm: {
        id: 0,
        bookName: '',
        bookVisit: '',
        bookHeat: '',
        info: '',
        type: '',
        isFree: '',
        categoryIds: [],
        categoryd: [],
        recommendId: '',
        publishTime: '',
        isAlive: ''
      },
      isAlive: '',
      projectList: [],
      dataRule: {
        // bookName: [
        //   { required: true, message: "书籍名称不能为空", trigger: "blur" },
        //   {  max: 14, message: '最长14个字符', trigger: 'blur' }
        // ],
        // bookHeat:[
        //    { required: true, message: "初始热度量不能为空", trigger: "blur" }
        // ],
        //  bookVisit:[
        //    { required: true, message: "初始阅读量不能为空", trigger: "blur" }
        // ],
        //  info:[
        //    { required: true, message: "推荐文案不能为空", trigger: "blur" },
        //     {  max: 40, message: '最长40个字符', trigger: 'blur' }
        // ],
      },
      url: ''
    }
  },
  mounted() {
    this.$http({
      url: this.$http.adornUrl(`/course/category/search`),
      method: 'get',
      params: this.$http.adornParams()
    }).then(({ data }) => {
      console.log(data)
      if (data && data.code === 0) {
        this.projectList = data.list
      }
    });
  },
  methods: {
    // 获取完成
    init(id) {
    this.dataForm.id = id || 0;
     this.visible = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/course/bookinfo/initUpOrDown/${this.dataForm.id}`
            ),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.bookName = data.upOrDownVo.bookName
              this.dataForm.bookHeat = data.upOrDownVo.bookHeat
              this.dataForm.bookVisit = data.upOrDownVo.bookVisit
              this.dataForm.type = data.upOrDownVo.type + ''
              this.dataForm.isFree = data.upOrDownVo.isFree + ''
              this.dataForm.info = data.upOrDownVo.info
              this.dataForm.categoryIds = data.upOrDownVo.categoryIds
              this.dataForm.recommendId = data.upOrDownVo.recommendId
              this.dataForm.isAlive = data.upOrDownVo.isAlive
              this.isAlive = data.upOrDownVo.isAlive
              this.dataForm.publishTime = data.upOrDownVo.publishTime
            }
          })
        }
      });
    },
    selectChange(value) {
      console.log(value)
      this.dataForm.categoryIds = value
    },
    changeTime () {
      if (new Date(this.dataForm.pushTime).getTime() < new Date().getTime()) {
        this.dataForm.pushTime = ''
        this.$message.error('所选时间要大于当前时间')
      }
    },
    // 表单提交
    // 上下架完成
    dataFormSubmit(n) {
      if(!this.dataForm.isAlive) {
        this.$message.error('请选择上架时间')
        return false
      }
      console.log(this.recommendId)
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/course/bookinfo/upOrDown`
            ),
            method: 'post',
            data: this.$http.adornData({
              bookId: this.dataForm.id,
              bookName: this.dataForm.bookName,
              briefIntroduction: this.dataForm.briefIntroduction,
              author: this.dataForm.author,
              categoryIds: this.dataForm.categoryIds,
              type: parseInt(this.dataForm.type),
              isFree: parseInt(this.dataForm.isFree),
              isAlive: (n === 1) ? this.dataForm.isAlive : 0,
              recommendId: this.dataForm.recommendId,
              publishTime: this.dataForm.publishTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              });
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }

  }
}
</script>
